<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <!-- 
        localStorage特点：即使关闭浏览器，存储的数据也不会消失。
     -->
    <button onclick="saveDate()">点我保存数据</button><br>
    <button onclick="readDate()">点我读取数据</button><br>
    <button onclick="deleteDate()">点我删除数据</button><br>
    <button onclick="deleteAllData()">点我清空数据</button>
    <script>
        let p = { name: '张三', age: 18 }

        function saveDate() {
            localStorage.setItem('msg', 'hello!')
            localStorage.setItem('person', JSON.stringify(p))
        }

        function readDate() {
            alert(localStorage.getItem('msg'))
            alert(localStorage.getItem('person'))
            console.log(localStorage.getItem('person'));
            console.log(JSON.parse(localStorage.getItem('person')));
        }

        function deleteDate() {
            if (confirm('确定删除？')) localStorage.removeItem('msg')
        }

        function deleteAllData() {
            if (confirm('确定清空所有数据？')) {
                localStorage.clear()
            }
        }
    </script>
</body>

</html>